#pragma once

#include <mutex>
#include <list>

template<class T>
class CMTList
{
public:
	typedef std::lock_guard<std::mutex> LockGuard;
	typedef std::list<T> ListType;
	typedef std::unique_ptr<ListType> ListPtr;

	CMTList() : m_cached_list(std::make_unique<ListType>())
	{}

	template<class... T>
	void Emplace(T&&... t)
	{
		LockGuard lck(m_mtx);
		m_cached_list->emplace_back(std::forward<T>(t)...);
	}

	ListPtr GetList()
	{
		LockGuard lck(m_mtx);
		if (m_cached_list->empty())
			return ListPtr();

		auto ret_list = std::move(m_cached_list);
		m_cached_list = std::make_unique<ListType>();
		return ret_list;
	}

protected:
	std::mutex m_mtx;
	ListPtr m_cached_list;
};
